package com.example.fangwei.utilsapp.alioss;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;

/**
 *
 *
 *
 * Created by fangw on 2018/4/16.
 */

public class OssManager {

    private OSS oss;
    private static OssManager instance;
    private String bucketName;


    private OssManager(Context context, String endpoint, String bucketName, String accessKeyId, String accessKeySecret){
        initOss(context, endpoint, bucketName, accessKeyId, accessKeySecret);
    }



    public static OssManager getOssManager(Context context, String endpoint, String bucketName, String accessKeyId, String accessKeySecret){
        if (null == instance){
            instance = new OssManager(context, endpoint, bucketName, accessKeyId, accessKeySecret);
        }
        return instance;
    }


    /**
     * 初始化oss
     * @param context           上下文对象
     * @param endpoint          地址
     * @param bucketName        文件夹名称
     * @param accessKeyId       阿里云账户分配的keyID
     * @param accessKeySecret   阿里云账户分配的keySecret
     * @return
     */
    private void initOss(Context context, String endpoint, String bucketName, String accessKeyId, String accessKeySecret) {

        if (null == oss){
            OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
            oss= new OSSClient(context, endpoint, credentialProvider);
        }
        this.bucketName = bucketName;
    }



    public void upload(String name,String filePath){
        OSSAsyncTask task;

        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(bucketName, name+".jpg", filePath);


        //上传进度
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                if (null != onUploadListener){
                    onUploadListener.onProgress(((int) (currentSize / totalSize)));
                }
            }
        });


        //上传操作的回调函数
        task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                if (null != onUploadListener){
                    onUploadListener.onUploadResult(true);
                }
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {
                if (null != onUploadListener){

//                    Log.e("OssManager", "ClientException-->" + clientException.getMessage() );
//                    Log.e("OssManager", "ServiceException-->" + serviceException.getMessage() );
                    onUploadListener.onUploadResult(false);
                }
            }
        });
    }


    private OnUploadListener onUploadListener;


    public void setOnUploadListener(OnUploadListener onUploadListener){
        this.onUploadListener = onUploadListener;
    }


    public interface OnUploadListener{
        void onProgress(int progress);

        void onUploadResult(boolean result);
    }

}
